package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.ContentAd;
import cn.net.shuxing.yifu.common.constant.OssConstant;
import cn.net.shuxing.yifu.common.mapper.ContentAdMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import cn.net.shuxing.yifu.common.util.StringUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.net.shuxing.yifu.common.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ContentAdService extends BaseService<ContentAd, ContentAdMapper> {

    @Autowired
    private OssCommonService ossCommonService;

    public List<ContentAd> getsNewest(Integer count)
    {
        CommonExample example = new CommonExample(ContentAd.class);
        example.setOrderByClause("update_time asc");
        int count1 = mapper.selectCountByExample(example);
        if (count1 < 5)
            return getsByExample(example);
        List<ContentAd> contentAdList = mapper.selectByExampleAndRowBounds(example, new RowBounds(1, 5));
        completeAll(contentAdList);
        return contentAdList;
    }

    @Override
    protected void complete(ContentAd bean) {
        String adPicture = bean.getAdPicture();
        if (StringUtils.hasText(adPicture))
        {
            bean.setAdPictureUrl(ossCommonService.getUrl(OssConstant.ossBucketName,adPicture));
        }
    }

    public List<ContentAd> getByAdState(int state) {
        CommonExample example = new CommonExample(ContentAd.class);
        example.equals("adState",state);
        return getsByExample
                (example);
    }

    public List<ContentAd> searchByAdName(String adName)
    {
        CommonExample example = new CommonExample(ContentAd.class);
        Example.Criteria criteria = example.getCriteria();
        criteria.andLike("adName","%"+adName+"%");
        return getsByExample(example);
    }
}
